//
//  NYSettingViewController.m
//  BS
//
//  Created by neal on 15/8/19.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYSettingViewController.h"
#import "NYSettingCachesCell.h"
#import <SDImageCache.h>
#import "NYSettingItem.h"
#import "NYSettingGroupItem.h"
#import "NYSettingArrowItem.h"
#import "NYSettingSwitchItem.h"
#import "NYSettingSegItem.h"
#import "NYSettingCell.h"

@interface NYSettingViewController ()
/* groups */
@property(nonatomic , strong) NSMutableArray *groups;

@end

@implementation NYSettingViewController

- (NSMutableArray *)groups
{
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}
static NSString *ID = @"Caches";

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.tableView.contentInset = UIEdgeInsetsMake(Margin - 35, 0, 0, 0);
    [self.tableView registerClass:[NYSettingCachesCell class] forCellReuseIdentifier:ID];
    [self setgroup0];
    [self setgroup1];
}

- (void)setgroup0
{
    NYSettingItem *item = [NYSettingSegItem itemWithTitle:@"字体大小"];
    [item setOpreationBlock:^{
        
    }];
    NYSettingItem *item1 = [NYSettingSwitchItem itemWithTitle:@"转发时收藏"];
    NYSettingItem *item2 = [NYSettingSwitchItem itemWithTitle:@"摇一摇夜间模式"];
    NYSettingGroupItem *group = [NYSettingGroupItem groupWtihItems:@[item,item1,item2] header:@"功能设置" footer:nil];
    [self.groups addObject:group];
}
- (void)setgroup1
{
    NYSettingItem *item = [NYSettingArrowItem itemWithTitle:@"离线下载"]
    ;
    NSString *cachepath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileSize = [cachepath stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"].fileSizeString;
    
    NYSettingItem *item1 = [NYSettingArrowItem itemWithTitle:[NSString stringWithFormat:@"清除缓存（已使用%@）",fileSize]];
    [item1 setOpreationBlock:^{
        [[SDImageCache sharedImageCache]clearDiskOnCompletion:^{
            [self.tableView reloadData];
        }];
    }];
    
    NYSettingItem *item2 = [NYSettingArrowItem itemWithTitle:@"推荐好友"];
    NYSettingItem *item3 = [NYSettingArrowItem itemWithTitle:@"帮助"];
    NSString *curVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    NYSettingItem *item4 = [NYSettingItem itemWithTitle:[NSString stringWithFormat:@"当前版本：%@",curVersion]];
    NYSettingItem *item5 = [NYSettingArrowItem itemWithTitle:@"关于我们"];
    NYSettingItem *item6 = [NYSettingArrowItem itemWithTitle:@"设备信息"];
    NYSettingItem *item7 = [NYSettingArrowItem itemWithTitle:@"隐私政策"];
    NYSettingItem *item8 = [NYSettingArrowItem itemWithTitle:@"打分支持不得姐！"];

    NYSettingGroupItem *group = [NYSettingGroupItem groupWtihItems:@[item,item1,item2,item3,item4,item5,item6,item7,item8] header:@"其他" footer:nil];
    [self.groups addObject:group];
}
//- (NSInteger)getFileSize
//{
////获取 cache 路径
//    NSString *cachespath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
//    NSLog(@"%@",cachespath);
//    
//    NSString *file = [cachespath stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"];
//    
//    NSFileManager *filemanager = [NSFileManager defaultManager];
//    
//    NSInteger size = 0 ;
//    
//    //获取子路径
//    NSArray *subpaths = [filemanager subpathsAtPath:file];
//    
//    for (NSString *subpath in subpaths) {
//        NSString *filepath = [file stringByAppendingPathComponent:subpath];
//        
//        NSDictionary *attrs = [filemanager attributesOfItemAtPath:filepath error:nil];
//
//        if ([attrs[NSFileType]isEqualToString:NSFileTypeDirectory]) {
//            continue;
//        }
//        size += [attrs[NSFileSize] integerValue];
//    }
//    NYLog(@"%zd",size);
//    return size;
//}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NYSettingGroupItem *group = self.groups[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //NYSettingCachesCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    NYSettingCell *cell = [NYSettingCell cellWithTableView:tableView];
    NYSettingGroupItem *group = self.groups[indexPath.section];
    NYSettingItem *item = group.items[indexPath.row];
    cell.item = item;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NYSettingGroupItem *group = self.groups[indexPath.section];
    NYSettingItem *item = group.items[indexPath.row];
    if (item.opreationBlock) {
        item.opreationBlock();
        return;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NYSettingGroupItem *group = self.groups[section];
    return group.headerTitle;
}

@end